package com.sx.jys.ui.frag

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.lxj.xpopup.XPopup
import com.sanxinsaas.nearstore.framework.util.click
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sos.widget.view.refresh.finishLoad
import com.sx.jys.R
import com.sx.jys.app.framework.BaseFragment
import com.sx.jys.databinding.FragCkPageBinding
import com.sx.jys.ui.act.CkDetailAct
import com.sx.jys.ui.adapter.CkListAdapter
import com.sx.jys.ui.dialog.DialogMakeSetting
import com.sx.jys.ui.vm.CkVm

class FragCkPage : BaseFragment<CkVm, FragCkPageBinding>() {
    companion object {
        fun getInstance(type: String? = null): Fragment {
            val fragCkPage = FragCkPage()
            val bundle = Bundle()
            type?.let { bundle.putString("type", type) }
            fragCkPage.arguments = bundle
            return fragCkPage
        }
    }

    var type: String = ""

    var page = 1


    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.ckRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                refreshLayout.finishRefresh(2500)
                mViewModel.reqCkList(page, type)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page += 1
                refreshLayout.finishLoadMore(2500)
                mViewModel.reqCkList(page, type)
            }
        })
        mDatabind.tvPc.click {
            XPopup.Builder(requireContext())
                .asConfirm("提示", "是否要一键平仓?") {
                    mViewModel.reqCloseAll(requireContext())
                }.show()
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.vm = mViewModel
        type = arguments?.getString("type") ?: ""
        mDatabind.adapter = CkListAdapter(requireContext(), type).also {
            it.setOnItemClickListener { viewId, item, position ->
                if (viewId == R.id.tv_pc) {
                    //平仓
                    XPopup.Builder(requireContext())
                        .asConfirm("提示", "是否要平仓?") {
                            mViewModel.reqCloseAll(requireContext(), item)
                        }.show()
                } else if (viewId == R.id.tv_make) {
                    DialogMakeSetting(requireContext()) { zy, zs ->
                        mViewModel.setProfitloss(requireContext(),item,zy,zs)
                    }
                        .show()
                }else if(viewId==R.id.tv_back){
                    XPopup.Builder(requireContext())
                        .asConfirm("提示", "是否要撤销?") {
                            mViewModel.reqOrderBack(requireContext(), item)
                        }.show()
                } else {
                    if (!"4".equals(type) && !"5".equals(type)) {
                        val intent = Intent(requireContext(), CkDetailAct::class.java)
                        intent.putExtra("status", type)
                        intent.putExtra("code", item.code)
                        intent.putExtra("openTime", item.openTime)
                        startActivity(intent)
                    }
                }

            }
        }
        if ("4".equals(type)) {
            //委托
            mDatabind.layoutTop.visibility = View.VISIBLE
            mDatabind.tvPc.visibility = View.GONE
            mDatabind.layoutOrder.visibility = View.VISIBLE
        } else if ("0".equals(type)) {
            //持仓中
            mDatabind.layoutTop.visibility = View.VISIBLE
            mDatabind.tvPc.visibility = View.VISIBLE
            mDatabind.layoutOrder.visibility = View.VISIBLE
        } else if ("1".equals(type) || "5".equals(type)) {
            //历史订单 已取消
            mDatabind.layoutTop.visibility = View.GONE
            mDatabind.tvPc.visibility = View.GONE
            mDatabind.layoutOrder.visibility = View.GONE
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.uiState.refreshEvent.observe(this) {
            mDatabind.ckRefresh.finishRefresh()
        }
        mViewModel.uiState.loadMoreEvent.observe(this) {
            mDatabind.ckRefresh.finishLoad(it)
        }
        mViewModel.uiState.autoRefreshEvent.observe(this) {
            mDatabind.ckRefresh.autoRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        page = 1
        type = arguments?.getString("type") ?: ""
        mViewModel.reqCkList(page, type)
        mViewModel.reqCkNum(type)
    }
}